package com.example.designpattern.ifelse;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.stream.Stream;

/**
 * @author long
 * @date 2024/11/13 11:37
 */
@AllArgsConstructor
public enum FruitEnum {

    APPLE(1,"apple",(qty,price) -> qty * price),

    BANANA(2,"banana",(qty,price) -> qty * price -1),

    ORANGE(3,"orange",(qty,price) -> qty * price + 1);

    @Getter
    private Integer code;

    @Getter
    private String desc;

    /**
     * 水果计算方法
     */
    @Getter
    private BiFunction<Integer,Double,Double> biFunction;

    /**
     * 根据code查询描述desc
     * @param code
     * @return
     */
    public static String getDescByCode(Integer code){
        return Stream.of(values()).filter(e-> Objects.equals(code,e.code))
                .findFirst().map(FruitEnum::getDesc).orElse(null);
    }

    public static FruitEnum getByDesc(String desc){
        return Stream.of(values()).filter(e-> Objects.equals(desc,e.desc))
                .findFirst().orElse(null);
    }

}
